package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.domain.UserRoleR;
import com.item.mapper.UserRoleRMapper;
import com.item.po.UserRoleRPo;
import com.item.service.UserRoleRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleRServiceImpl implements UserRoleRService {

    @Autowired
    UserRoleRMapper userRoleRMapper;


    public List<UserRoleRPo> findAll(int limit, int page, UserRoleR userRoleR) {
        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        List<UserRoleRPo> all = userRoleRMapper.findAll(userRoleR);
        for (UserRoleRPo userRoleRPo : all) {
            userRoleRPo.setMenuIds(userRoleRPo.getMenuId().split(","));
        }
        return all;
    }


    public int add(UserRoleR userRoleR) {

        if (userRoleR.getUserName() != null) {
            int i = 0;
            try {
                i = userRoleRMapper.insertSelective(userRoleR);
            } catch (Exception e) {
                return i;
            }
            return i;
        }
        return 0;
    }


    public int update(UserRoleR userRoleR) {
        int i = 0;
        try {
            i = userRoleRMapper.updateByPrimaryKeySelective(userRoleR);
        } catch (Exception e) {
            return i;
        }
        return i;
    }

    @Override
    public int delete(int id) {
        return userRoleRMapper.deleteByPrimaryKey(id);
    }
}
